package wxk.interview.aop.core.jdk.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author wangxuekai
 * @version 1.0
 * @date 2017/3/5.
 */
public class MySimpleHandler implements InvocationHandler {
    private Object target;

    public MySimpleHandler(){
        super();
    }

    public MySimpleHandler(Object target){
        this.target = target;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("--------before--------");
        Object result = method.invoke(target, args);
        System.out.println("--------after--------");
        return result;
    }

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public Object getProxy() {
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this);
    }
}
